/**
 * @class Ext.form.field.DateRange
 * @extends Ext.form.Field
 * <p>A combination field that includes date and end dates and times, as well as an optional all-day checkbox.</p>
 * @constructor
 * @param {Object} config The config object
 */
Ext.define('DutyList.calendar.form.field.DateRange', {
    extend: 'Ext.form.FieldContainer',
    alias: 'widget.daterangefield',
    
    requires: [
        'Ext.form.field.Date',
        'Ext.form.field.Time',
        'Ext.form.Label',
        'Ext.form.field.Checkbox',
        'Ext.layout.container.Column'
    ],
    toText: 'to',
    /**
     * @cfg {String} dateFormat
     * The date display format used by the date fields (defaults to 'n/j/Y') 
     */
    dateFormat: 'n/j/Y',

    // private
    fieldLayout: {
        type: 'hbox',
        defaultMargins: { top: 0, right: 5, bottom: 0, left: 0 }
    },
    
    // private
    initComponent: function() {
        var me = this;
        
        me.addCls('ext-dt-range');
        me.layout = me.fieldLayout;
        me.items = me.getFieldConfigs();
        me.callParent(arguments);
        me.initRefs();
    },
    
    initRefs: function() {

        var me = this;
        me.startDate = me.down('#' + me.id + '-startDate');
        me.night = me.down('#' + me.id + '-night');
        me.endDate = me.down('#' + me.id + '-endDate');

        me.startDate.validateOnChange = false;
        me.endDate.validateOnChange = false;
    },

    getFieldConfigs: function() {
        var me = this;
        return [
            me.getStartDateConfig(),
            me.getDateSeparatorConfig(),
            me.getEndDateConfig(),
            me.getNightDayConfig()
        ];
    },

    getDateSeparatorConfig: function() {
        return {
            xtype: 'label',
            itemId: this.id + '-to-label',
            text: this.toText,
            margins: { top: 4, right: 5, bottom: 0, left: 0 }
        };
    },

    getStartDateConfig: function() {
        return {
            xtype: 'datefield',
            itemId: this.id+'-startDate',
            format: this.dateFormat,
            flex: 5,
            listeners: {
                'blur': {
                    fn: function(){
                        this.onFieldChange();
                    },
                    scope: this
                }
            }
        };
    },

    getEndDateConfig: function() {
        return {
            xtype: 'datefield',
            itemId: this.id+'-endDate',
            format: this.dateFormat,
            flex: 5,
            listeners: {
                'blur': {
                    fn: function(){
                        this.onFieldChange();
                    },
                    scope: this
                }
            }
        };
    },
    
    getNightDayConfig: function() {
        return {
            xtype: 'checkbox',
            itemId: this.id + '-night',
            hidden: false,
            boxLabel: 'night',
            margins: { top: 2, right: 5, bottom: 0, left: 0 },
            flex: 2,
            scope: this
        };
    },
  // private todo
    onFieldChange: function(){
        this.fireEvent('change', this, this.getValue());
    },
        
    // private getValue helper
    //
    getDT: function(end){
        var el = this[(end ===true ? 'endDate' :'startDate')],
            dt = el.getValue();
        if(Ext.isDate(dt)){
            dt = Ext.Date.format(dt, el.format);
        }
        else{
            return null;
        };
        return Ext.Date.parseDate(dt, el.format);
        
    },
    // private setValue helper
    setDT: function(dt,end){
        if(!dt) {
            return;
        }
        if(dt && Ext.isDate(dt)){
            this[(end ===true ? 'endDate' :'startDate')].setValue(dt);
            return true;
        }
    },
    getNight: function(){
        return this['night'].getValue();
    },
    // private setValue helper
    setNight: function(dt){
        this['night'].setValue(dt);
        return true;
    },

    // inherited docs
    isDirty: function(){
        var dirty = false;
        if(this.rendered && !this.disabled) {
            this.items.each(function(item){
                if (item.isDirty()) {
                    dirty = true;
                    return false;
                }
            });
        }
        return dirty;
    },

    getValue: function(){
        return [
            this.getDT(),
            this.getNight(),
            this.getDT(true)
        ];
    },
    // private
    onDisable : function(){
        this.delegateFn('disable');
    },
    
    // private
    onEnable : function(){
        this.delegateFn('enable');
    },
    
    // inherited docs
    reset : function(){
        this.delegateFn('reset');
    },
    
    // private
    delegateFn : function(fn){
        this.items.each(function(item){
            if (item[fn]) {
                item[fn]();
            }
        });
    },
    
    // private
    beforeDestroy: function(){
        this.callParent(arguments);
    },
    
    /**
     * @method getRawValue
     * @hide
     */
    getRawValue : Ext.emptyFn,
    /**
     * @method setRawValue
     * @hide
     */
    setRawValue : Ext.emptyFn
});